home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 1.iso
/
ARGONET
/
PD
/
MATHS
/
RLAB
/
RLAB125.ZIP
/
!RLaB
/
examples
/
curve_fit
< prev
next >
Wrap
Text File
|
1995-05-20
|
1KB
|
64 lines
//
// curve_fit.r
//
// Use a minimization scheme to fit a model to a set of data
// points.
// Create the data...
require rem
x = (0:10:.1)';
y = 3.2.*1.5.^x;
pltitle ("Original Contrived Data");
plot ([x,y]);
pause ();
// Now add some random-ness
rand ("uniform", -max(abs(y))/5, max(abs(y))/5);
y_orig = y;
y = y + rand (size (y));
pltitle ("Original Data with some Random-ness Added");
plot ([x,y]);
pause ();
// Now make up a model function
fmodel = function (a, b, x) { return a.*b.^x; }
// Now make up a cost function
fcost = function ( A )
{
global (x, y)
return -sum ( abs ((y - fmodel (A[1], A[2], x))).*x);
}
// Now load the min/max - imizer
rfile mdsmax
rfile nmsmax
#fit = nmsmax (fcost, [1, 1]);
fit = mdsmax (fcost, [1, 1]);
// Plot the result
pltitle ("Original+Random-ness and Fitted Model");
plot ([ x, y, fmodel (fit.x[1], fit.x[2], x) ]);
pause ();
pltitle ("Original and Fitted Model");
plot ([ x, y_orig, fmodel (fit.x[1], fit.x[2], x) ]);
"\tNow compare original model coefficients and fitted"
original = [3.2, 1.5]
fitted = fit.x
difference = [3.2, 1.5] - fit.x